package utils

import (
	"github.com/kataras/iris"
	"imooc.com/demo_pms/comm"
	"imooc.com/demo_pms/models"
	"imooc.com/demo_pms/web/viewmodels"
	"log"
	"time"
)

func ParamString(ctx iris.Context) (sql string) {
	work_id := ctx.FormValueDefault("work_id", "")
	username := ctx.FormValueDefault("username", "")
	occupation := ctx.FormValueDefault("occupation", "")
	part := ctx.FormValueDefault("part", "")
	beginTime := ctx.FormValueDefault("beginTime", "")
	endTime := ctx.FormValueDefault("endTime", "")
	beginWages := ctx.FormValueDefault("beginWages", "")
	endWages := ctx.FormValueDefault("endWages", "")
	gender := ctx.FormValueDefault("gender", "")

	strs := make([]string, 9)
	strs[0] = work_id
	strs[1] = username
	strs[2] = occupation
	strs[3] = part
	strs[4] = beginTime
	strs[5] = endTime
	strs[6] = beginWages
	strs[7] = endWages
	strs[8] = gender

	sql = comm.NewSelectSql(strs)
	return sql
}

func DbUserToString(datalist []models.UserInfo) []string {
	rs := []string{}

	return rs
}

func ViewUserToDbUser(data *viewmodels.ViewUserInfo) *models.UserInfo {
	rs := &models.UserInfo{}

	if data != nil {
		rs.WorkId = data.Work_id
		rs.Name = data.Name
		rs.Sex = data.Sex
		rs.Part = data.Part
		rs.Occupation = data.Occupation
		rs.CreateTime = data.Time
		rs.SysStatus = 1
		rs.UpdateTime = int(time.Now().Unix())
	}

	return rs
}

// 测试过，转换成功
func ExcelToDbInfo(rows [][]string) ([]models.UserInfo, []models.Payroll) {
	userList := []models.UserInfo{}
	payList := []models.Payroll{}
	if len(rows) < 1 {
		log.Println("rows为空")
		return userList, payList
	}
	for _, row := range rows[1:] {
		// 表格有7列，长度别搞错了
		if len(row) == 7 {
			userData := models.UserInfo{}
			payData := models.Payroll{}
			userData, payData = sliceStringToDbInfo(row)
			userList = append(userList, userData)
			payList = append(payList, payData)
		}
	}
	return userList, payList
}

func sliceStringToDbInfo(row []string) (models.UserInfo, models.Payroll) {
	work_id := comm.StrToInt(row[0])
	sex := comm.SexToInt(row[2])
	createTime := comm.StrToInt(row[5])
	wages := comm.StrToInt(row[6])

	userData := models.UserInfo{
		WorkId:     work_id,
		Name:       row[1],
		Sex:        sex,
		Part:       row[3],
		Occupation: row[4],
		CreateTime: createTime,
		SysStatus:  1,
		UpdateTime: createTime,
	}
	payData := models.Payroll{
		WorkId: work_id,
		Time:   createTime,
		Wages:  wages,
	}

	return userData, payData
}
